#include "SalesmanGreedyChromosomeFactory.h"
#include "SalesmanChromosome.h"
#include "parameters.h"

SalesmanGreedyChromosomeFactory::SalesmanGreedyChromosomeFactory(int _chromosomeLength, SalesmanData _salesmanData) :
                    AbstractChromosomeFactory(_chromosomeLength),
                    salesmanData(_salesmanData),
                    index(0)
{
    TRACE_FUNC
}

Chromosome* SalesmanGreedyChromosomeFactory::getNewChromosome()
{
    TRACE_FUNC
    return getNewChromosome(chromosomeLength);
}

Chromosome* SalesmanGreedyChromosomeFactory::getNewChromosome(int len)
{
    TRACE_FUNC_ENTRY

    SalesmanChromosome* chrom = new SalesmanChromosome(0,len-1);
    QList<int> pathList;

    int cityInd = index;

    for(int i=0;i<len;++i)
    {
        QList<double> distances = salesmanData.getCityDistanceVector(cityInd).toList();
        qSort(distances.begin(), distances.end());
        int shortestWayInd = salesmanData.getCityDistanceVector(cityInd).indexOf(distances.first());
        while(pathList.contains(shortestWayInd))
        {
            distances.removeFirst();//TODO list size
            shortestWayInd = salesmanData.getCityDistanceVector(cityInd).indexOf(distances.first());
        }

        pathList.append(shortestWayInd);
        cityInd = shortestWayInd;
    }

    chrom->setData(pathList);

    qDebug() << *chrom;

    index++;
    if(index >= salesmanData.size())
        index = 0;

    TRACE_FUNC_EXIT
    return chrom;
}

Chromosome* SalesmanGreedyChromosomeFactory::getNewRandomChromosome()
{
    return getNewChromosome(chromosomeLength);
}
